package com.lvcoding.sec3.secdemo3.config;

import com.google.common.collect.Lists;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger2配置类
 * @author  wuyanshen
 */
@EnableOpenApi
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.OAS_30)
                .globalRequestParameters(Lists.newArrayList(
                        new RequestParameterBuilder()
                                .name("token")
                                .description("header请求中放入token")
                                .query(parameterSpecificationBuilder -> parameterSpecificationBuilder.defaultValue("1")
                                        .allowEmptyValue(true))
                                .in(ParameterType.HEADER)
                                .build()
                ))
                // .groupName("全部接口")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //为当前包下controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com.lvcoding.sec3.secdemo3.controller"))
                .build();
    }



    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("接口文档，描述词省略200字")
                .version("1.0")
                .build();
    }
}
